package com.mlearning.demo.controller;

import com.mlearning.demo.pojo.neo4j.User;
import com.mlearning.demo.service.TreeService;
import com.mlearning.demo.service.UserService;
import com.mlearning.demo.util.EncryptUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.util.Date;

@Controller
@RequestMapping("/func")
public class FuncController {
    private Date date = new Date();
    @Autowired
    private TreeService treeService;
    @Autowired
    private UserService userService;

    @RequestMapping("/delMastered")
    public void delMastered(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        int userid = ((User) session.getAttribute("user")).getId();
        String know = request.getParameter("know");
        if (treeService.delMastered(userid, know) > 0) {
            response.getWriter().write("{\"message\":200}");
        } else {
            response.getWriter().write("{\"message\":-1}");
        }
    }

    @RequestMapping("/addMastered")
    public void addMastered(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        int userid = ((User) session.getAttribute("user")).getId();
        String know = request.getParameter("know");
        if (treeService.addMastered(userid, know, date.getTime()) > 0) {
            response.getWriter().write("{\"message\":200}");
        } else {
            response.getWriter().write("{\"message\":-1}");
        }
    }
    @RequestMapping("/getMastered")
    public void getMastered(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        int userid = ((User) session.getAttribute("user")).getId();
        response.getWriter().write(userService.updateMastered(userid));
    }
    @RequestMapping("/delKnowledge")
    public void delMyKnowledge(HttpServletRequest request,HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        int userid=((User)session.getAttribute("user")).getId();
        String know=request.getParameter("know");
        int tree=Integer.parseInt(request.getParameter("tree"));
        int no=Integer.parseInt(request.getParameter("no"));
        int total=Integer.parseInt(request.getParameter("total"));
        //先从树上删除该知识
        if(treeService.delMyKnowledge(userid,tree,no)>0){
            response.getWriter().write("{\"message\":200}");
        }
        else{
            response.getWriter().write("{\"message\":-1}");
        }
//        如果是该树上唯一一个知识，删除后需要靠后的树编号需要减去1，即删除整棵树
        if(total==1){
            treeService.updateTreeNo(userid,tree);
        }
//        如果不是该树上最后一个知识，需要将靠后的知识编号减去1
        if(no<total){
            treeService.updateKnowledgeNo(userid,tree,no);
        }
    }
    @RequestMapping("/addKnowledge")
    public void addMyKnowledge(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        int userid=((User)session.getAttribute("user")).getId();
        String know=request.getParameter("know");
        int tree=Integer.parseInt(request.getParameter("tree"));
        int no=Integer.parseInt(request.getParameter("no"));
        if(tree>5 || no>10){
            return;
        }
//        System.out.println(userid);
        if(treeService.addMyKnowledge(userid,know,tree,no,date.getTime())>0){
            response.getWriter().write("{\"message\":200}");
        }
        else{
            response.getWriter().write("{\"message\":-1}");
        }
    }
    @RequestMapping("/addMybook")
    public void addMybook(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        User user=(User)session.getAttribute("user");

        int bookid=Integer.parseInt(request.getParameter("bookid"));
        if(userService.addMyBook(bookid, user.getId(),date.getTime())>0){
            response.getWriter().write("{\"message\":200}");
        }
        else{
            response.getWriter().write("{\"message\":-1}");
        }
    }
    @RequestMapping("/delMyBook")
    public void delMyBook(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        User user=(User)session.getAttribute("user");
        int bookid=Integer.parseInt(request.getParameter("bookid"));
        if(userService.delMyBook(bookid, user.getId())>0){
            response.getWriter().write("{\"message\":200}");
        }
        else{
            response.getWriter().write("{\"message\":-1}");
        }
    }
    @RequestMapping("/getMyBooks")
    public void getMyBooks(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        int userid = ((User) session.getAttribute("user")).getId();
        response.getWriter().write(userService.updateMyBooks(userid));
    }
    @RequestMapping("/edit")
    public void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        User user=(User)session.getAttribute("user");
        int sort=Integer.parseInt(request.getParameter("sort"));
        String content=request.getParameter("content");
//        if(userService.edit(user.getId(),sort,content) >0){
        switch (sort) {
            case 1:
                if (user.getName().equals(content)) {
                    response.getWriter().write("{\"message\":0}");
                    return;
                }
                ((User) session.getAttribute("user")).setName(content);
                break;
            case 2:
                if (user.getSign().equals(content)) {
                    response.getWriter().write("{\"message\":0}");
                    return;
                }
                ((User) session.getAttribute("user")).setSign(content);
                break;
            case 3:
                ((User) session.getAttribute("user")).setEmail(content);
                break;//修改邮箱需要新邮箱验证
            case 4:
                content = EncryptUtil.getSHA256StrJava(content);
                break;//修改密码需要删除session重新登陆
        }
        userService.edit(user.getId(), sort, content);
        response.getWriter().write("{\"message\":200}");
//        }
//        else{
//            response.getWriter().write("{\"message\":-1}");
//        }
    }
    @RequestMapping("/getTrees")
    public void getTrees(HttpServletRequest request, HttpServletResponse response)throws IOException{
        HttpSession session = request.getSession();
        User user=(User)session.getAttribute("user");
        response.getWriter().write( treeService.getMyTrees(user.getId()));
    }
    @RequestMapping("/getMyTrees")
    public void getMyTrees(HttpServletRequest request, HttpServletResponse response)throws IOException{
        HttpSession session = request.getSession();
        User user=(User)session.getAttribute("user");
        response.getWriter().write( userService.updateMyTrees(user.getId()));
    }
}
